package com.example.a10405.tmp.activity;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.model.LatLng;
import com.example.a10405.tmp.R;
import com.example.a10405.tmp.model.Point;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.bmapView)
    MapView mapView; //绑定地图控件

    public BaiduMap baiduMap; //地图实例

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //在使用SDK各组件之前初始化context信息，传入ApplicationContext
        // 注意该方法要再setContentView方法之前实现
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);

        baiduMap = mapView.getMap();

        LatLng latLng = new LatLng(41.802273,123.417315);//将地图移至沈阳市
        MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(latLng);
        baiduMap.animateMapStatus(u);

        addMarkerOnMap();
    }


    /**
     * 在地图上加载标注点
     */
    public void addMarkerOnMap() {

        for(Point point : Point.pointList) {
            //在地图上添加标注点marker
            LatLng latLngMarker = new LatLng( point.getLatitude(), point.getLongitude());// 标注点的经纬度

            BitmapDescriptor mIconMaker = BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher);
            OverlayOptions overlayOptions = new MarkerOptions()//添加图片标注
                    .position(latLngMarker)
                    .icon(mIconMaker)
                    .zIndex(5);// 图标
            Marker marker = (Marker) (baiduMap.addOverlay(overlayOptions));
            OverlayOptions textOption = new TextOptions()//添加文字标注
                    .position(latLngMarker)
                    .text(point.getName())
                    .fontSize(42)
                    .fontColor(Color.rgb(0, 150, 64));
            baiduMap.addOverlay(textOption);

            //绑定marker的数据
            Bundle bundle = new Bundle();
            bundle.putSerializable("name", point.getName());
            bundle.putSerializable("pictures", point.getImgList());
            marker.setExtraInfo(bundle);

        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mapView.onDestroy();
    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mapView.onPause();
    }
}

